package com.li.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.li.interfaces.OnTopNavigationBarListener;
import com.ndboo.reslibrary.R;

/**
 * Author: Li
 * Date: 2017/9/12 0012
 * Description: 头部
 */

public class TopNavigationBar extends RelativeLayout {
    public TopNavigationBar(Context context) {
        this(context, null);
    }

    private ImageView mImgBack;
    private TextView mTxtTitle;
    private ImageView mImgFunction;
    private TextView mTxtFunction;
    private OnTopNavigationBarListener mNavigationBarListener;

    private void init(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_top_navigation, this);
        mImgBack = view.findViewById(R.id.img_back);
        mImgFunction = view.findViewById(R.id.img_function);
        mTxtTitle = view.findViewById(R.id.txt_title);
        mTxtFunction = view.findViewById(R.id.txt_function);
        mImgBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mNavigationBarListener != null) {
                    mNavigationBarListener.onBackClickListener(view);
                }
            }
        });
        mImgFunction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mNavigationBarListener != null) {
                    mNavigationBarListener.onFunctionImgListener(v);
                }
            }
        });
        mTxtFunction.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mNavigationBarListener != null) {
                    mNavigationBarListener.onFunctionTxtListener(v);
                }
            }
        });
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TopNavigationBar);
        String title = array.getString(R.styleable.TopNavigationBar_navigation_title);
        String functionTxt = array.getString(R.styleable.TopNavigationBar_navigation_txt_function);
        setFunctionTxt(functionTxt);
        setTitle(title);
        int visible = array.getInt(R.styleable.TopNavigationBar_navigation_arrow_visible, 0);
        switch (visible) {
            case View.VISIBLE:
                mImgBack.setVisibility(View.VISIBLE);
                break;
            case View.INVISIBLE:
                mImgBack.setVisibility(View.INVISIBLE);
                break;
            case View.GONE:
                mImgBack.setVisibility(View.GONE);
                break;
        }
        int functionVisible = array.getInt(R.styleable.TopNavigationBar_navigation_function_visible, View.GONE);
        switch (functionVisible) {
            case View.VISIBLE:
                mImgFunction.setVisibility(View.VISIBLE);
                break;
            case View.INVISIBLE:
                mImgFunction.setVisibility(View.INVISIBLE);
                break;
            case View.GONE:
                mImgFunction.setVisibility(View.GONE);
                break;
        }


        int txtFunctionVisible = array.getInt(R.styleable.TopNavigationBar_navigation_txt_function_visible, View.GONE);

        switch (txtFunctionVisible) {
            case View.VISIBLE:
                mTxtFunction.setVisibility(View.VISIBLE);
                break;
            case View.INVISIBLE:
                mTxtFunction.setVisibility(View.INVISIBLE);
                break;
            case View.GONE:
                mTxtFunction.setVisibility(View.GONE);
                break;
        }
        int imgRes=array.getResourceId(R.styleable.TopNavigationBar_navigation_img, R.drawable.ic_setting);
        setFunctionImg(imgRes);
        array.recycle();


    }

    public TopNavigationBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public void setTxtFunctionVisible(int visible){
        mTxtFunction.setVisibility(visible);
    }
    public void setNavigationBarListener(OnTopNavigationBarListener navigationBarListener) {
        mNavigationBarListener = navigationBarListener;
    }

    public void setTitle(String txt) {
        mTxtTitle.setText(txt);
    }

    public void setTitle(int res) {
        mTxtTitle.setText(res);
    }

    public void setFunctionTxt(String functionTxt) {
        mTxtFunction.setText(functionTxt);
    }
    public void setFunctionImg(int res){
        mImgFunction.setImageResource(res);
    }
    public String getFunctionTxt(){
        return mTxtFunction.getText().toString();
    }
}
